//
//  PzNorPhotoCtrl.swift
//  Pz
//
//  Created by 吴斌 on 2024/7/4.
//

import Foundation
class PzNorPhotoCtrl: UIViewController {
    var photoSubMdls:NSArray?
    var photoSubSearchMdls:NSMutableArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = true
        view.backgroundColor = .clear
        creatSubView()
        defPhotoData()
    }
    lazy var photoBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.isUserInteractionEnabled = true
        imageView.image = UIImage(named: "pzHomeBg")
        return imageView
    }()
    lazy var photoNavBackBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(vipNavBack), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzNavBackIcon"), for: .normal)
        return btn
    }()
    lazy var photoNavTitleLab: UILabel = {
        let lab = UILabel()
        lab.text = "证件照尺寸查询"
        lab.font = UIFont.systemFont(ofSize: 34.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0xFFFFFF)
        return lab
    }()
    lazy var norPhotoSearchView: PzNorPhotoSearchView = {
        let view = PzNorPhotoSearchView()
        return view
    }()
    
    lazy var norPhotoTableView: PzNorPhotoTableView = {
        let view = PzNorPhotoTableView()
        return view
    }()
    func creatSubView(){
        self.view.addSubview(photoBgImgView)
        self.view.addSubview(photoNavBackBtn)
        self.view.addSubview(photoNavTitleLab)
        self.view.addSubview(norPhotoSearchView)
        self.view.addSubview(norPhotoTableView)

        photoBgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        photoNavBackBtn.snp.makeConstraints { make in
            make.left.equalTo(24.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 12.fitScale())
            make.width.height.equalTo(64.fitScale())
        }
        photoNavTitleLab.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(photoNavBackBtn)
            make.height.equalTo(50.fitScale())
        }
        norPhotoSearchView.snp.makeConstraints { make in
            make.left.equalTo(30.fitScale())
            make.right.equalTo(-30.fitScale())
            make.top.equalTo(112.fitScale()+PzPublicConst.ScreenTopSpaceHeight)
            make.height.equalTo(72.fitScale())
        }
        norPhotoSearchView.norPhotoSearchBlock = { searchText in
            self.view.endEditing(false)
            self.searchPhoto(searchText: searchText)
        }
        
        norPhotoTableView.snp.makeConstraints { make in
            make.left.equalTo(0.fitScale())
            make.right.equalTo(0.fitScale())
            make.top.equalTo(208.fitScale()+PzPublicConst.ScreenTopSpaceHeight)
            make.bottom.equalTo(-0.fitScale())
        }
        norPhotoTableView.norPhotoBlock = { photoSubMdl in
            let photoCtrl = PzPhotoCtrl()
            photoCtrl.photoSubMdl = photoSubMdl
            self.navigationController?.pushViewController(photoCtrl, animated: true)
        }
        let norPhotoTap = UITapGestureRecognizer(target: self, action: #selector(norPhotoEnd))
        self.view.addGestureRecognizer(norPhotoTap);
    }
    func searchPhoto(searchText:String?) {
        if searchText == ""{
            photoSubSearchMdls = NSMutableArray(array: photoSubMdls ?? NSArray())
        }else{
            photoSubSearchMdls = NSMutableArray()
            for i in 0..<(photoSubMdls?.count ?? 0) {
                let photoSubDic = photoSubMdls?[i] as? NSDictionary
                let name:String = photoSubDic?["name"] as? String ?? ""
                
                if name.contains(searchText ?? ""){
                    photoSubSearchMdls?.add(photoSubDic as Any)
                }
            }
        }
        self.norPhotoTableView.photoSubMdls = photoSubSearchMdls
    }
    @objc func norPhotoEnd(){
        self.view.endEditing(false)
    }
    func defPhotoData(){
        let filePath = Bundle.main.path(forResource: "PzPhoto", ofType: "plist")
        let plists = NSArray(contentsOfFile: filePath!)
        photoSubMdls = plists
        photoSubSearchMdls = NSMutableArray(array: plists ?? NSArray())
        self.norPhotoTableView.photoSubMdls = photoSubSearchMdls
    }
    @objc func vipNavBack(){
        self.navigationController?.popViewController(animated: true)
    }
}
